import vtkActor from "@kitware/vtk.js/Rendering/Core/Actor";
import { BActor } from "../Core/BActor";
import { ACTOR_TYPE } from "../";
import equal from "fast-deep-equal";
import vtkMapper from "@kitware/vtk.js/Rendering/Core/Mapper";
import vtkWidgetManager from "@kitware/vtk.js/Widgets/Core/WidgetManager";
import vtkInteractorStyleTrackballCamera from "@kitware/vtk.js/Interaction/Style/InteractorStyleTrackballCamera";
//@ts-ignore
import vtkLineWidget from "@kitware/vtk.js/Widgets/Widgets3D/LineWidget";
import vtkRenderer from "@kitware/vtk.js/Rendering/Core/Renderer";

export enum HandleShapes {
  SPHERE = "sphere",
  CONE = "cone",
  CUBE = "cube",
  TRIANGLE = "triangle",
  FOUR_POINTS_ARROW_HEAD = "4pointsArroHead",
  SIX_POINTS_ARROW_HEAD = "6pointsArrowHead",
  STAR = "star",
  DISK = "disk",
  CIRCLE = "circle",
  VIEWFINDER = "viewFinder",
  NONE = "voidSphere",
}
export class LineWidget extends BActor {
  readonly type = ACTOR_TYPE.WIDGET;
  createdBy = "default";
  instance: vtkActor = vtkActor.newInstance();
  mapper: vtkMapper = vtkMapper.newInstance();
  widgetManager: vtkWidgetManager = vtkWidgetManager.newInstance();
  lineWidget: any;
  private _selectedWidgetIndex = 0;
  private _getHandle: any[] = [];
  renderer: vtkRenderer;

  constructor(renderer: vtkRenderer) {
    super();
    this.renderer = renderer;
    this.addWidget();
  }

  addWidget() {
    this.renderer.resetCamera();
    // 释放焦点
    this.widgetManager.releaseFocus();
    let currentHandle: vtkLineWidget = null;
    const renderWindow = this.renderer.getRenderWindow();
    let interactor = null;
    if (renderWindow) {
      interactor = renderWindow.getInteractor();
      interactor.setInteractorStyle(
        vtkInteractorStyleTrackballCamera.newInstance()
      );
      // 渲染窗口
      renderWindow.render();
    }
    this.widgetManager.setRenderer(this.renderer);
    // 创建 LineWidget
    const widget = vtkLineWidget.newInstance();
    currentHandle = this.widgetManager.addWidget(widget);

    this.lineWidget = currentHandle;
    this.selectWidget(this.widgetManager.getWidgets().length - 1);

    this._getHandle = [
      this.lineWidget.getWidgetState().getHandle1(),
      this.lineWidget.getWidgetState().getHandle2(),
    ];

    this.widgetManager.enablePicking();
    this.widgetManager.grabFocus(currentHandle);
    currentHandle.onStartInteractionEvent(() => {
      const index = this.widgetManager.getWidgets().findIndex((cwidget) => {
        if (equal(currentHandle.getWidgetState(), cwidget.getWidgetState()))
          return 1;
        return 0;
      });
      this._getHandle = [
        currentHandle.getWidgetState().getHandle1(),
        currentHandle.getWidgetState().getHandle2(),
      ];
      this.selectWidget(index);
      this.lineWidget = currentHandle;
    });
    setTimeout(() => {
      this.updateHandleShape(0, HandleShapes.CUBE);
      this.updateHandleShape(1, HandleShapes.STAR);
    }, 2000);
  }

  setWidgetColor(currentWidget: any, color: number) {
    currentWidget.getWidgetState().getHandle1().setColor(color);
    currentWidget.getWidgetState().getHandle2().setColor(color);

    currentWidget.setUseActiveColor(false);
    currentWidget.getWidgetState().getMoveHandle().setColor(0.3);
  }

  // Restore color
  unselectWidget(index: number) {
    if (index != null) {
      const widgetToUnselect =
        this.widgetManager.getWidgets()[this._selectedWidgetIndex];
      this.setWidgetColor(widgetToUnselect, 0.5); // green
    }
    if (index === this._selectedWidgetIndex) {
      this._selectedWidgetIndex = 0;
    }
  }

  selectWidget(index: number) {
    this.unselectWidget(this._selectedWidgetIndex);
    if (index != null) {
      const widgetToSelect = this.widgetManager.getWidgets()[index];
      this.setWidgetColor(widgetToSelect, 0.2); // yellow
    }
    this._selectedWidgetIndex = index;
  }
  updateHandleShape(handleId: number, shape: string) {
    const renderWindow = this.renderer.getRenderWindow();
    if (this._getHandle.length !== 0) {
      const handle = this._getHandle[handleId];
      if (handle) {
        console.log("handle", handle, shape);

        handle.setShape(shape);
        this.lineWidget.updateHandleVisibility(handleId);
        this.lineWidget.getInteractor().render();
        if (renderWindow) {
          renderWindow.render();
        }
      }
    }
  }
}
